home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / INFO / DOSTRAIN.ZIP / TTT.BAS < prev    next >
BASIC Source File  |  1985-05-01  |  11KB  |  299 lines

  1. 10  REM TTT
  2. 20  REM This program plays tic-tac-toe with the user.
  3. 30  REM
  4. 40  REM Variable          Explanation
  5. 50  REM BDROW             upper left row of the board
  6. 60  REM BDCOL             upper left col of the board
  7. 70  REM SCROW             top row of scorecard display
  8. 80  REM INTROW            interaction area row
  9. 90  DIM BR (9)'           row coordinates of centers of squares for display
  10. 100 DIM BC (9)'           column coordinates of centers of squares for display
  11. 110 REM BD1$, BD2$        constants used in drawing the board
  12. 120 REM PLAYERNM$         the player's name
  13. 130 REM MAXNML            maximum name length of "The cat (ties)" or PLAYERNM$
  14. 140 REM PLS$              player's symbol
  15. 150 REM CPS$              computer's symbol
  16. 160 REM PW                the number of games the player has won so far
  17. 170 REM CW                the number of games the computer has won so far
  18. 180 REM CATW              the number of cat's games (ties) so far
  19. 190 DIM BOARD (9)'        current tic-tac-toe board, with values as follow:
  20. 200 REM                         0 = untaken square
  21. 210 REM                         1 = player has the square
  22. 220 REM                        -1 = computer has the square
  23. 230 REM PLAYS             the number of plays so far this game
  24. 240 REM TURN              switch: if 0, computer is next, if 1, the player
  25. 250 REM WINNER            the winner of the current game:
  26. 260 REM                         0 = no winner so far
  27. 270 REM                         1 = the player won
  28. 280 REM                        -1 = the computer won
  29. 290 REM ERRTYPE           error types for players move
  30. 300 REM                         1 = choice out of 1-9 range
  31. 310 REM                         2 = square already taken
  32. 320 REM MORE              0 if the user is ready to quit, 1 for a new game
  33. 330 REM
  34. 500 REM Main program
  35. 510 CLS
  36. 520 GOSUB 1000'           initialize screen format parameters
  37. 530 GOSUB 1500'           ask user for options
  38. 540 GOSUB 2000'           display rules, if requested
  39. 550 GOSUB 2500'           display skeleton board
  40. 560 GOSUB 3000'           display scoreboard
  41. 570 GOSUB 3500'           clear the internal board
  42. 580 GOSUB 4000'           display cleared board
  43. 590 IF TURN = 0 THEN GOSUB 4500: TURN = 1 ELSE GOSUB 5000: TURN = 0
  44. 600 GOSUB 4000'           display board
  45. 610 GOSUB 5500'           check for a winner
  46. 620 IF PLAYS < 9 AND WINNER = 0 THEN GOTO 590'  no winner yet
  47. 630 GOSUB 6000'           display winner's name and update score
  48. 640 GOSUB 6500'           display the score
  49. 650 GOSUB 7000'           check for more games
  50. 660 IF MORE = 1 THEN GOTO 570
  51. 670 GOSUB 7500'           display ending message
  52. 680 STOP
  53. 690 REM
  54. 1000 REM Initialize the screen format parameters.
  55. 1010 REM
  56. 1020 REM Outputs:  BDROW, BDCOL, SCROW, INTROW, BR(), and BC()
  57. 1030 REM
  58. 1040 BDROW = 2: BDCOL = 45
  59. 1050 SCROW = 2: INTROW = 18
  60. 1060 BR(1)=1: BR(2)=1: BR(3)=1: BR(4)=5: BR(5)=5: BR(6)=5: BR(7)=9:                  BR(8)=9: BR(9)=9
  61. 1070 BC(1)=1: BC(2)=9: BC(3)=17: BC(4)=1: BC(5)=9: BC(6)=17: BC(7)=1:                BC(8)=9: BC(9)=17
  62. 1080 FOR I = 1 TO 9
  63. 1090    BR(I) = BR(I) + BDROW
  64. 1100    BC(I) = BC(I) + BDCOL + 5
  65. 1110 NEXT I
  66. 1120 RETURN
  67. 1130 REM
  68. 1500 REM Input the user-selected options.
  69. 1510 REM
  70. 1520 REM OUTPUTS:  PLAYERNM$, MAXNML, TURN, CPS$, AND PLS$
  71. 1530 REM
  72. 1540 INPUT "What's your name? ", PLAYERNM$
  73. 1550 PLAYERNM$ = LEFT$ (PLAYERNM$, 76 - BDCOL)
  74. 1560 MAXNML = LEN (PLAYERNM$) + 4
  75. 1570 IF MAXNML < 14 THEN MAXNML = 14
  76. 1580 PRINT
  77. 1590 INPUT "Do you want to go first? ", TEMP$
  78. 1600 IF LEFT$(TEMP$,1) = "n" OR LEFT$(TEMP$,1) = "N" THEN TURN = 0 ELSE TURN =1
  79. 1610 PRINT
  80. 1620 IF TURN = 0 THEN PRINT "Okay, I'll take X, you've got O." ELSE                     PRINT "Okay, I'll take O and you have X."
  81. 1630 IF TURN = 0 THEN CPS$ = "X": PLS$ = "O" ELSE CPS$ = "O": PLS$ = "X"
  82. 1640 PRINT
  83. 1650 RETURN
  84. 1660 REM
  85. 2000 REM Display the rules of the game, if requested by the user.
  86. 2010 REM
  87. 2020 INPUT "Would you like to see the rules? ", TEMP$
  88. 2030 CLS
  89. 2040 IF LEFT$ (TEMP$, 1) = "n" OR LEFT$ (TEMP$, 1) = "N" THEN RETURN
  90. 2050 PRINT
  91. 2060 PRINT "   The idea of the game is to get three squares in a horizontal,"
  92. 2070 PRINT "vertical or diagonal row.  The board is layed out like this:"
  93. 2080 PRINT
  94. 2090 PRINT TAB(25)  "1 : 2 : 3"
  95. 2100 PRINT TAB(25)  "--:---:--"
  96. 2110 PRINT TAB(25)  "4 : 5 : 6"
  97. 2120 PRINT TAB(25)  "--:---:--"
  98. 2130 PRINT TAB(25)  "7 : 8 : 9"
  99. 2140 PRINT
  100. 2150 PRINT "When the computer asks for your move, just enter the number of"
  101. 2160 PRINT "the square you want to take."
  102. 2170 PRINT
  103. 2180 INPUT "Hit ENTER when you have finished reading the rules.", TEMP$
  104. 2190 CLS
  105. 2200 RETURN
  106. 2210 REM
  107. 2500 REM Display the board outline.
  108. 2510 REM
  109. 2520 REM Inputs are BDROW and BDCOL.
  110. 2530 REM
  111. 2540 REM Variable       Explanation
  112. 2550 REM BD1$           first line used in drawing the board
  113. 2560 REM BD2$           second line used in drawing the board
  114. 2570 REM
  115. 2580 BD1$ = "          :       :"
  116. 2590 BD2$ = "  --------+-------+-------"
  117. 2600 FOR I = 0 TO 10
  118. 2610    LOCATE BDROW + I, BDCOL
  119. 2620    IF I = 3 OR I = 7 THEN PRINT BD2$ ELSE PRINT BD1$
  120. 2630 NEXT I
  121. 2640 RETURN
  122. 2650 REM
  123. 3000 REM Display the scoreboard.
  124. 3010 REM
  125. 3020 REM Inputs are SCROW and MAXNML
  126. 3030 REM
  127. 3040 LOCATE SCROW, 1
  128. 3050 TEMP = (MAXNML +10) / 2 - 5
  129. 3060 PRINT TAB(TEMP); "Scoreboard"
  130. 3070 PRINT
  131. 3080 PRINT "Player"; TAB(MAXNML + 4) + "Games"
  132. 3090 PRINT TAB(MAXNML + 5) + "Won"
  133. 3100 PRINT
  134. 3110 PRINT PLAYERNM$ + " (" + PLS$ + ")"
  135. 3120 PRINT "Computer (" + CPS$ + ")"
  136. 3130 PRINT "The cat (ties)"
  137. 3140 RETURN
  138. 3150 REM
  139. 3500 REM Clear board and reset the number of plays this game
  140. 3510 REM
  141. 3520 REM Outputs:  BOARD() AND PLAYS
  142. 3530 REM
  143. 3540 PLAYS = 0
  144. 3550 FOR I=1 TO 9
  145. 3560    BOARD (I) = 0
  146. 3570 NEXT I
  147. 3580 RETURN
  148. 3590 REM
  149. 4000 REM Display the current board.
  150. 4010 REM
  151. 4020 REM Inputs are BOARD(), BR(), BC(), PLS$, and CPS$
  152. 4030 REM
  153. 4040 FOR I = 1 TO 9
  154. 4050    LOCATE BR (I), BC (I)
  155. 4060    IF BOARD (I) = 1 THEN PRINT PLS$;
  156. 4070    IF BOARD (I) = 0 THEN PRINT " ";
  157. 4080    IF BOARD (I) = -1 THEN PRINT CPS$;
  158. 4090 NEXT I
  159. 4100 RETURN
  160. 4110 REM
  161. 4500 REM The computer chooses its next move.
  162. 4510 REM
  163. 4520 REM Inputs are BOARD() and PLAYS.
  164. 4530 REM Outputs are BOARD() and PLAYS.
  165. 4540 REM
  166. 4550 PLAYS = PLAYS + 1' count the turn
  167. 4560 IF BOARD (5) = 0 THEN BOARD (5) = -1: RETURN'      try the center
  168. 4570 FOR I = 1 TO 9 STEP 2'                             try a corner
  169. 4580    IF BOARD (I) = 0 THEN BOARD (I) = -1: RETURN
  170. 4590 NEXT I
  171. 4600 FOR I = 2 TO 8 STEP 2'                             take a side
  172. 4610    IF BOARD (I) = 0 THEN BOARD (I) = -1: RETURN
  173. 4620 NEXT I
  174. 4630 REM
  175. 5000 REM This subroutine gets a move from the player.
  176. 5010 REM It does not return until a valid move is entered.
  177. 5020 REM
  178. 5030 REM Inputs are INTROW, PLAYS, and BOARD().
  179. 5040 REM Outputs are PLAYS and BOARD().
  180. 5050 REM
  181. 5060 REM Variable       Explanation
  182. 5070 REM ERRTYPE        type of user error
  183. 5080 REM                   1 = selection out of 1-9 range
  184. 5090 REM                   2 = tried to take an occupied square
  185. 5100 REM
  186. 5110 PLAYS = PLAYS + 1' count the turn
  187. 5120 LOCATE INTROW, 1
  188. 5130 GOSUB 8500'        clear the current display line
  189. 5140 INPUT "Which square do you want? ", TEMP$
  190. 5150 TEMP = VAL (TEMP$)
  191. 5160 IF TEMP < 1 OR TEMP > 9 THEN ERRTYPE = 1: GOSUB 8000: GOTO 5120
  192. 5170 IF BOARD (TEMP) <> 0 THEN ERRTYPE = 2: GOSUB 8000: GOTO 5120
  193. 5180 BOARD (TEMP) = 1
  194. 5190 GOSUB 9000'         clear the interaction area
  195. 5200 RETURN
  196. 5210 REM
  197. 5500 REM Check to see if someone has won.  If so WINNER
  198. 5510 REM is set and the appropriate win count is incremented.
  199. 5520 REM
  200. 5530 REM Input is BOARD()
  201. 5540 REM Output is WINNER
  202. 5550 REM
  203. 5560 REM check for a row winner
  204. 5570 FOR I = 1 TO 7 STEP 3
  205. 5580   TEMP = BOARD (I) + BOARD (I+1) + BOARD (I+2)
  206. 5590   IF TEMP = 3 THEN WINNER = 1: RETURN
  207. 5600   IF TEMP = -3 THEN WINNER = -1: RETURN
  208. 5610 NEXT I
  209. 5620 REM check for a column winner
  210. 5630 FOR I = 1 TO 3
  211. 5640   TEMP = BOARD (I) + BOARD (I+3) + BOARD (I+6)
  212. 5650   IF TEMP = 3 THEN WINNER = 1: RETURN
  213. 5660   IF TEMP = -3 THEN WINNER = -1: RETURN
  214. 5670 NEXT I
  215. 5680 REM check for diagonal winner
  216. 5690 TEMP = BOARD (3) + BOARD (5) + BOARD (7)
  217. 5700 IF TEMP = 3 THEN WINNER = 1: RETURN
  218. 5710 IF TEMP = -3 THEN WINNER = -1: RETURN
  219. 5720 TEMP = BOARD (1) + BOARD (5) + BOARD (9)
  220. 5730 IF TEMP = 3 THEN WINNER = 1: RETURN
  221. 5740 IF TEMP = -3 THEN WINNER = -1: RETURN
  222. 5750 REM no winner so far
  223. 5760 WINNER = 0: RETURN
  224. 5770 REM
  225. 6000 REM The game is over.  Display winner and update score.
  226. 6010 REM
  227. 6020 REM Inputs are WINNER, PW, CW, CATW, PLAYERNM$, and INTROW.
  228. 6030 REM Outputs are PW, CW, and CATW.
  229. 6040 REM
  230. 6050 LOCATE INTROW, 1
  231. 6060 IF WINNER = 1 THEN PW = PW+1:                                                   PRINT "You got me that time, "; PLAYERNM$;".
  232. 6070 IF WINNER = -1 THEN CW=CW+1:                                                    PRINT "I beat you this game, "; PLAYERNM$; ".
  233. 6080 IF WINNER = 0 THEN CATW = CATW+1:                                               PRINT "This one is a tie game "; PLAYERNM$; "."
  234. 6090 RETURN
  235. 6100 REM
  236. 6500 REM Display the current scores.
  237. 6510 REM
  238. 6520 REM Inputs are SCROW, MAXNML, CW, PW, AND CATW.
  239. 6530 REM
  240. 6540 LOCATE SCROW + 5, MAXNML + 5
  241. 6550 PRINT PW;
  242. 6560 LOCATE SCROW + 6, MAXNML + 5
  243. 6570 PRINT CW;
  244. 6580 LOCATE SCROW + 7, MAXNML + 5
  245. 6590 PRINT CATW;
  246. 6600 RETURN
  247. 6610 REM
  248. 7000 REM Check to see if the player wants another game.  Return
  249. 7010 REM with MORE = 1 if so, otherwise return with MORE = 0.
  250. 7020 REM
  251. 7030 REM Input is INTROW.
  252. 7040 REM Output is MORE.
  253. 7050 REM
  254. 7060 LOCATE INTROW + 2, 1
  255. 7070 LINE INPUT "Do you want to play again (ENTER = yes)? ", TEMP$
  256. 7080 MORE = 1
  257. 7090 IF LEFT$ (TEMP$,1) = "n" OR LEFT$ (TEMP$,1) = "N" THEN MORE = 0
  258. 7100 GOSUB 9000'        clear interaction area
  259. 7110 RETURN
  260. 7120 REM
  261. 7500 REM Display the final message.
  262. 7510 REM
  263. 7520 REM Inputs are PLAYERNM$, PW and CW.
  264. 7530 REM
  265. 7540 LOCATE INTROW, 1
  266. 7550 PRINT "Thanks for playing "; PLAYERNM$; "."
  267. 7560 IF CW = PW THEN PRINT "I guess we're about even."
  268. 7570 IF CW > PW THEN PRINT "Sorry to have humiliated you."
  269. 7580 IF PW > CW THEN PRINT "You showed me a thing or two."
  270. 7590 RETURN
  271. 7600 REM
  272. 8000 REM Display error message.
  273. 8010 REM
  274. 8020 REM Inputs are INTROW and ERRTYPE.
  275. 8030 REM
  276. 8040 LOCATE INTROW + 2, 1
  277. 8050 GOSUB 8500'         clear the line
  278. 8060 IF ERRTYPE = 1 THEN PRINT "Your choice must be a number between 1 and 9."
  279. 8070 IF ERRTYPE = 2 THEN PRINT "Don't try to fool me; that square is taken."
  280. 8080 RETURN
  281. 8090 REM
  282. 8500 REM Clear the current line and leave the cursor unmoved.
  283. 8510 REM
  284. 8520 TEMP = CSRLIN'   save the original cursor line
  285. 8530 LOCATE TEMP, 1'  place cursor at left edge of line
  286. 8540 PRINT SPACE$ (80);
  287. 8550 LOCATE TEMP, 1'  restore cursor to original line
  288. 8560 RETURN
  289. 8570 REM
  290. 9000 REM Clear the interaction area on the screen.
  291. 9010 REM
  292. 9020 REM Input is INTROW.
  293. 9030 REM
  294. 9040 LOCATE INTROW, 1
  295. 9050 FOR I = 1 TO 3
  296. 9060    PRINT SPACE$ (80)
  297. 9070 NEXT I
  298. 9080 RETURN
  299.